#ifndef __UTIL_INCLUDE__
#define __UTIL_INCLUDE__

#include <unistd.h>
#include <sys/stat.h>

class Util {
public:
  static char *Lstrip(char *s) {
    while (*s != '\0' &&
           (*s == ' ' || *s == '\t' ||
            *s == '\r' || *s == '\n')) {
      s++;
    }
    return s;
  }

  static char *Rstrip(char *s) {
    size_t len = strlen(s);
    char *end = s + len - 1;
    while (len-- && 
           (*end == ' ' || *end == '\n' ||
            *end == '\t' || *end == '\r')) {
      end--;
    }
    *(++end) = '\0';
  }

  static int64_t buff2long(const char *buff) {
    unsigned char *p = (unsigned char *)buff;
      return  (((int64_t)(*p)) << 56) | \
          (((int64_t)(*(p+1))) << 48) | \
          (((int64_t)(*(p+2))) << 40) | \
          (((int64_t)(*(p+3))) << 32) | \
          (((int64_t)(*(p+4))) << 24) | \
          (((int64_t)(*(p+5))) << 16) | \
          (((int64_t)(*(p+6))) << 8) | \
          ((int64_t)(*(p+7)));
  }

  static void long2buff(int64_t n, char *buff) {
    unsigned char *p;
    p = (unsigned char *)buff;
    *p++ = (n >> 56) & 0xFF;
    *p++ = (n >> 48) & 0xFF;
    *p++ = (n >> 40) & 0xFF;
    *p++ = (n >> 32) & 0xFF;
    *p++ = (n >> 24) & 0xFF;
    *p++ = (n >> 16) & 0xFF;
    *p++ = (n >> 8) & 0xFF;
    *p++ = n & 0xFF;
  }

  static bool FileExists(const char *filename) {
    return access(filename, 0) == 0;
  }

  static bool IsDir(const char *filename) {
    struct stat buf;
    if (stat(filename, &buf) != 0) {
      return false;
    }

    return S_ISDIR(buf.st_mode);
  }
};

#endif
